package com.jz.digital.view;

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;

import com.jz.digital.base.EditInputCallback;

/**
 * 数字、字母、下划线、连接符、点、@
 */
public class EmailInputFilter implements InputFilter {
    private EditInputCallback editInputCallback;

    public EmailInputFilter() {
    }

    public EmailInputFilter(EditInputCallback editInputCallback) {
        this.editInputCallback = editInputCallback;
    }

    public void setEditInputCallback(EditInputCallback editInputCallback) {
        this.editInputCallback = editInputCallback;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if (!valid(source, start, end)) return "";
        int lineCode1 = "_".codePointAt(0);
        int lineCode2 = "@".codePointAt(0);
        int lineCode3 = ".".codePointAt(0);
        int lineCode4 = " ".codePointAt(0);
        int lineCode5 = "-".codePointAt(0);
        int count = end - dend;//等于1是依次输入，大于1是复制粘贴
        if (count > 1 || (end > 1 && dstart == dend)) {
            StringBuilder charSequence = new StringBuilder();
            boolean isFirst = true;
            for (int i = 0; i < source.length(); i++) {
                if (source.length() > i) {
                    int cp = Character.codePointAt(source, i);
                    if (isChinese(source.charAt(i)) || lineCode4 == cp || (!Character.isLetter(cp) && !Character.isDigit(cp) && lineCode1 != cp && lineCode2 != cp && lineCode3 != cp && lineCode5 != cp)) {
//                        source = source.subSequence(0, i);
//                        CommonLogUtil.d("kelina====3=" + source);
                        if (editInputCallback != null && isFirst) {
                            isFirst = false;
                            editInputCallback.errorTip();
                        }
                    } else {
                        charSequence.append(source.charAt(i));
                    }
                }
            }
            return charSequence;

        } else {
            int cp = Character.codePointAt(source, end - 1);
            if (isChinese(source.charAt(end - 1)) || lineCode4 == cp || (!Character.isLetter(cp) && !Character.isDigit(cp) && lineCode1 != cp && lineCode2 != cp && lineCode3 != cp && lineCode5 != cp)) {
                if (editInputCallback != null) {
                    editInputCallback.errorTip();
                }
                return source.subSequence(0, end - 1);
            }
        }
        return source;

    }

    /**
     * 参数是否合法
     */
    private boolean valid(CharSequence text, int start, int end) {
        boolean success = true;
        if (TextUtils.isEmpty(text) || start < 0 || end < 0 || start >= end || start >= text.length() || end > text.length()) {
            success = false;
        }
        return success;
    }

    private boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}
